<?php
/*************************************************************************************/
/*this class contains all functions that interact with visitors' logging*/

class ClsLog {

/*************************************************************************************/

/*get Time Zone from Client's local date,
backurl specifies the url the browser loads after getting Time Zone*/
function GetTZ($backurl) {

print "<html>\n\n<body>\n";
print "<form name='myForm' action='$backurl' method='POST'>\n";
print "<input type='hidden' name='tz'>\n";
print "</form>\n";

// Isolate Time Zone out of date

print "<script language='JavaScript'> <!--\n";
print "now = new Date();\n";
print "gmt = -1/60*now.getTimezoneOffset();\n";
print "gmtsav = gmt;\n";
print "gmt = Math.floor(Math.abs(gmt)) + '.' + Math.floor(60*(Math.abs(gmt)-Math.floor(Math.abs(gmt))));\n";
print "(gmtsav>0)?pref='GMT+':pref='GMT-';\n";
print "gmt = pref + gmt.toString();\n";
print "// Isolate year string from date\n";
print "document.myForm.tz.value = gmt;\n";
print "document.myForm.submit();\n";
print "//-->\n";
print "</script>\n</body>\n\n</html>";
}

/*************************************************************************************/

/*set a	cookie LOGID if not already set*/
function SetCke1() {

/*if no	cookie is present, set one*/
//reset($GLOBALS);
global $LOGID;

$IsCke=0;
//while (list($key, $value) = each($GLOBALS)) {
//	if (ereg('LOGID',$key)) {
	$value=$LOGID;
	if ($LOGID != "") {
		$IsCke=1;
		//break;
	}
//}
$ret=0;

if(!$IsCke) {
	/*connect database*/
	$myDb =	new Db;
	$myDb->connect();

	/*get id*/
	$qry="SELECT logid FROM id";
	$result=mysql_query($qry) or die("Error: couldn't execute query 1 ($qry)");
	
	$mId=mysql_result($result,0,'logid');
	$value=	'logid'	. $mId;
	$LOGID=$value;
	
	/*set cookie (lifetime=365 days)*/
	SetCookie('LOGID',$value,time()+31536000, "/",$GLOBALS[HOST_NAME]);

	/*increment id.logid*/
	$qry="UPDATE id	SET logid=logid+1";
	$result=mysql_query($qry) or die("Error: couldn't execute query 2 ($qry)");

	/*close	database connection*/		
	$myDb->close();
	
	$ret=1;
}
return $ret;
}
/*************************************************************************************/

/*this function	insert a line into log*/
function InsLg($action,$info_sup) {

global $SMARTACCESS,$LOGID;

/*if smart access is on, return now, do not log*/
if($SMARTACCESS == 1) return;

/*connect database*/
$myDb =	new Db;
$myDb->connect();

/*see whether current hours' quota (60 a hour) is not reached*/
/*$date=date("Y-m-d H");
$qry="SELECT date FROM log WHERE date LIKE '$date%'";
$result=mysql_query($qry) or die("Error: couldn't execute query 3 ($qry)");

if (strcmp($result,""))
	$number=mysql_numrows($result);
else
	$number=0;
*/
$number=0;
/*if not reached, proceed!*/
if ($number<60)
	{
	$date=date("Y-m-d H:i:s");

	if($LOGID != "") {
		$value=$LOGID;
		$info_sup = "$value,$info_sup";
	}
	$qry="INSERT INTO log VALUES('$date','$action','$info_sup','".getenv("REMOTE_ADDR")."','')";
	$result=mysql_query($qry) or die("Error: couldn't execute query 4 ($qry)");
	}
/*close	database connection*/		
$myDb->close();

}
/*************************************************************************************/

/*redirect browser to $url via JavaScript function*/
function redirect($url) {

print "<html>\n\n<head>\n<script Language='JavaScript'> <!--\n";
print "function redirect() {\n";
print "document.location = '$url';\n}\n";
print "//-->\n";
print "</script>\n</head>\n\n";
print "<body onload='redirect()'>\n";
print "\n</body>\n\n</html>\n";
}

/*End of class*/
};
?>
